<?php
class ReturnSaveEditAction extends UserAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('return','edit');
	}
	
	public function execute()
	{
		$this->data['return_setting']	= C('SettingService')->get('return_setting',true);
		
		$data					= $this->getValidData();
		$data['user_id']		= $this->data['user']['id'];
		$data['alipay_account']	= $this->data['user']['alipay_account'];
		
		//检查返现金额下限
		if($data['money']<$this->data['return_setting']['return_min'])
		{
			$this->data['error_messages']['money']	= array('min'=>'返现金额不能少于最小返现金额');
		}
		//检查是否有足够的可返现金额
		$returnService					= new ReturnService();
		$money							= $this->data['user']['money'];
		if($data['money']>$money)
		{
			$this->data['error_messages']['money']	= array('max'=>'返现金额不能超过可返现金额');
		}
		
		if(!empty($this->data['error_messages']))
		{
			$this->code			= 407;
		}
		else
		{
			$id					= $returnService->add($data);
			if($id)
			{
				if(!C('UserService')->addMoney($this->data['user']['id'],0-$data['money']))
				{
					$returnService->del($id);
				}
				$this->code	= 200;
				$this->data['forward'] = C('LtUrl')->generate('Return', 'Index');
			}
			else
			{
				$this->code	= 400;
			}
		}
	}
}
